﻿using Elasticsearch.Net;
using Log.Api.Extensions;
using Log.Api.IService;
using Microsoft.Extensions.Options;
using Nest;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Log.Api.Service
{
    public class EsClientProviderService : IEsClientProviderService
    {
        private readonly IOptions<EsConfig> config;

        public EsClientProviderService(IOptions<EsConfig> config)
        {
            this.config = config;
        }

        /// <summary>
        /// 指定index获取ElasticClient
        /// </summary>
        /// <param name="indexName"></param>
        /// <returns></returns>
        public ElasticClient GetClient(string indexName)
        {
            if (config == null || config.Value == null || config.Value.Urls == null || !config.Value.Urls.Any())
            {
                throw new Exception("urls can not be null");
            }

            return GetClient(config.Value.Urls.ToArray(), indexName);
        }

        /// <summary>
        /// 根据url获取ElasticClient
        /// </summary>
        /// <param name="url"></param>
        /// <param name="defaultIndex"></param>
        /// <returns></returns>
        private ElasticClient GetClient(string url, string defaultIndex = "")
        {
            if (string.IsNullOrWhiteSpace(url))
            {
                throw new Exception("es 地址不可为空");
            }

            var uri = new Uri(url);
            var connectionSetting = new ConnectionSettings(uri);
            if (!string.IsNullOrWhiteSpace(url))
            {
                connectionSetting.DefaultIndex(defaultIndex);
            }

            return new ElasticClient(connectionSetting);
        }

        /// <summary>
        /// 根据urls获取ElasticClient
        /// </summary>
        /// <param name="urls"></param>
        /// <param name="defaultIndex"></param>
        /// <returns></returns>
        private ElasticClient GetClient(string[] urls, string defaultIndex = "")
        {
            if (urls == null || !urls.Any())
            {
                throw new Exception("urls can not be null");
            }

            var uris = urls.Select(p => new Uri(p)).ToArray();

            if (uris == null || uris.Length < 1)
            {
                throw new Exception("uris can not be null");
            } 

            ConnectionSettings connectionSetting;
            if (uris.Length == 1)
            {
                var connectionPool = new SingleNodeConnectionPool(uris.FirstOrDefault());
                connectionSetting = new ConnectionSettings(connectionPool);
            }
            else
            {
                var connectionPool = new SniffingConnectionPool(uris);
                connectionSetting = new ConnectionSettings(connectionPool);
            }

            if (!string.IsNullOrWhiteSpace(defaultIndex))
            {
                defaultIndex = defaultIndex.ToLower();
                connectionSetting.DefaultIndex(defaultIndex);
            }

            ////connectionSetting.BasicAuthentication("", ""); //设置账号密码
            return new ElasticClient(connectionSetting);
        }
    }
}
